package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.DynamicPraiseNoticeBean;
import com.beiing.tianshuai.tianshuai.util.DateUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by magikliu on 2017/9/28.
 */

public class PraiseNotice_RVAdapter extends RecyclerView.Adapter<PraiseNotice_RVAdapter.mViewHolder> {

    private Context mContext;
    private List<DynamicPraiseNoticeBean.DataBean.NumberBean> mData;
    private LayoutInflater layoutInflater;
    private RequestOptions requestOptions;
    private OnItemViewClickListener mOnItemViewClickListener;

    public PraiseNotice_RVAdapter(Context mContext, List<DynamicPraiseNoticeBean.DataBean.NumberBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
        layoutInflater = LayoutInflater.from(mContext);
        requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(false)
                .centerCrop()
                .override(350, 350);
    }

    @Override
    public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_notice_praise, parent, false);
        return new mViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final mViewHolder holder, int position) {
        /*  显示部分  */
        Glide.with(mContext)
                .load(TextUtils.isEmpty(mData.get(position).getPhoto()) ? R.mipmap.default_avatar
                        : HttpRequestConstant.USER_PIC_HEAD + mData.get(position).getPhoto())
                .apply(requestOptions)
                .into(holder.civAvatarPraise);
        holder.tvName.setText(mData.get(position).getName());
        holder.tvTime.setText(DateUtil.getTime(mData.get(position).getCreateTime()));
        if (!TextUtils.isEmpty(mData.get(position).getTable_type())) {
            switch (mData.get(position).getTable_type()) {
                case "0":
                    holder.tvPraise.setText("赞了这条动态");
                    break;
                case "1":
                    holder.tvPraise.setText("赞了这条评论");
                    break;
                case "2":
                    holder.tvPraise.setText("赞了这条动态");  //转发
                    break;
                case "3":
                    holder.tvPraise.setText("赞了这条评论");  //转发
                    break;
            }
        }
        if (TextUtils.isEmpty(mData.get(position).getPicname())) {
            holder.ivDynamicPic.setVisibility(View.GONE);
        } else {
            holder.ivDynamicPic.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .asBitmap()
                    .load(HttpRequestConstant.DYNAMIC_PIC_HEAD + mData.get(position).getPicname())
                    .apply(requestOptions)
                    .into(holder.ivDynamicPic);
        }

        /*
         *  点击事件部分
         */
        holder.civAvatarPraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemViewClickListener.onHeaderClick(view, holder.getAdapterPosition(), mData.get(holder.getAdapterPosition()));
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemViewClickListener != null){
                    mOnItemViewClickListener.onContentClick(view, holder.getAdapterPosition(),mData.get(holder.getAdapterPosition()));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public interface OnItemViewClickListener {
        void onHeaderClick(View view, int position, DynamicPraiseNoticeBean.DataBean.NumberBean bean);

        void onReplyClick(View view, int position, DynamicPraiseNoticeBean.DataBean.NumberBean bean);

        void onContentClick(View view, int position, DynamicPraiseNoticeBean.DataBean.NumberBean bean);
    }

    public void setOnItemViewClickListener(OnItemViewClickListener listener) {
        this.mOnItemViewClickListener = listener;
    }

    class mViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.civ_avatar_praise)
        QMUIRadiusImageView civAvatarPraise;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_praise)
        TextView tvPraise;
        @BindView(R.id.iv_dynamic_pic)
        QMUIRadiusImageView ivDynamicPic;

        public mViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
